home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / eig.cc < prev    next >
C/C++ Source or Header  |  1996-11-03  |  2KB  |  116 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include "EIG.h"
  28.  
  29. #include "defun-dld.h"
  30. #include "error.h"
  31. #include "gripes.h"
  32. #include "help.h"
  33. #include "oct-obj.h"
  34. #include "utils.h"
  35.  
  36. DEFUN_DLD (eig, args, nargout,
  37.   "eig (X) or [V, D] = eig (X): compute eigenvalues and eigenvectors of X")
  38. {
  39.   octave_value_list retval;
  40.  
  41.   int nargin = args.length ();
  42.  
  43.   if (nargin != 1 || nargout > 2)
  44.     {
  45.       print_usage ("eig");
  46.       return retval;
  47.     }
  48.  
  49.   octave_value arg = args(0);
  50.  
  51.   int nr = arg.rows ();
  52.   int nc = arg.columns ();
  53.  
  54.   int arg_is_empty = empty_arg ("eig", nr, nc);
  55.   if (arg_is_empty < 0)
  56.     return retval;
  57.   else if (arg_is_empty > 0)
  58.     return octave_value_list (2, Matrix ());
  59.  
  60.   if (nr != nc)
  61.     {
  62.       gripe_square_matrix_required ("eig");
  63.       return retval;
  64.     }
  65.  
  66.   Matrix tmp;
  67.   ComplexMatrix ctmp;
  68.   EIG result;
  69.  
  70.   if (arg.is_real_type ())
  71.     {
  72.       tmp = arg.matrix_value ();
  73.  
  74.       if (error_state)
  75.     return retval;
  76.       else
  77.     result = EIG (tmp);
  78.     }
  79.   else if (arg.is_complex_type ())
  80.     {
  81.       ctmp = arg.complex_matrix_value ();
  82.  
  83.       if (error_state)
  84.     return retval;
  85.       else
  86.     result = EIG (ctmp);
  87.     }
  88.   else
  89.     {
  90.       gripe_wrong_type_arg ("eig", tmp);
  91.       return retval;
  92.     }
  93.  
  94.   if (nargout == 0 || nargout == 1)
  95.     {
  96.       retval(0) = result.eigenvalues (), 1;
  97.     }
  98.   else
  99.     {
  100.       // Blame it on Matlab.
  101.  
  102.       ComplexDiagMatrix d (result.eigenvalues ());
  103.  
  104.       retval(1) = d;
  105.       retval(0) = result.eigenvectors ();
  106.     }
  107.  
  108.   return retval;
  109. }
  110.  
  111. /*
  112. ;;; Local Variables: ***
  113. ;;; mode: C++ ***
  114. ;;; End: ***
  115. */
  116.